﻿
//> public void addEvent()
function addEvent(element, type, handler) {

	// create a hash table of event type for the element.
	if (!element.events)
		element.events = {};

	var handlers = element.events[type];
	// console.log(element.id + ' handler.$$guid ' + handler.$$guid);
	// event의 unique id를 할당한다.
	if (!handler.$$guid)
		handler.$$guid = addEvent.guid++;

	if (!handlers) {

		handlers = element.events[type] = {};
		if (element["on" + type]) {
			handlers[0] = element["on" + type];
		}
	}
	// event handler를 hashtable에 저장한다.
	handlers[handler.$$guid] = handler;
	// 전역 event handler를 모든 element에 할당한다.
	element["on" + type] = handleEvent;
	// console.log('add event '+ type + ":" + handler.$$guid + " " + element.id
	// );

}
// unique id
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.events && element.events[type]) {
		delete element.events[type][handler.$$guid];
	}
}
function handleEvent(event) {
	var returnValue = true;
	// IE 는 global event object를 사용한다.
	event = event || fixEvent(window.event);

	var handlers = this.events[event.type];

	for ( var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			return false;
		}
	}
	return returnValue;

}

function fixEvent(event) {
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropageation;
	return event;
}

fixEvent.preventDefault = function() {
	this.returnValue = false;
}
fixEvent.stopPropageation = function() {
	this.cancelBubble = true;
}
